package org.geektimes.projects.user.bean.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MaxLengthValidator implements ConstraintValidator<MaxLength, CharSequence> {
    private Long maxValue;

    @Override
    public void initialize(MaxLength maxLength) {
        this.maxValue = maxLength.value();
    }

    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        //null values are valid
        if ( value == null ) {
            return true;
        }

        return value.length() < maxValue;
    }
}
